<style>
    #push_me {
        position: relative;
        color: rgba(255,255,255,1);
        text-decoration: none;
        background-color:rgb(0, 122, 255);
        font-family: 'Yanone Kaffeesatz';
        font-weight: 700;
        font-size: 3em;
        display: block;
        padding: 4px;
        -webkit-border-radius: 8px;
        -moz-border-radius: 8px;
        border-radius: 8px;
        -webkit-box-shadow: 0px 9px 0px rgb(10, 100, 255), 0px 9px 25px rgba(0,0,0,.7);
        -moz-box-shadow: 0px 9px 0px rgb(10, 100, 255), 0px 9px 25px rgba(0,0,0,.7);
        box-shadow: 0px 9px 0px rgb(10, 100, 255), 0px 9px 25px rgba(0,0,0,.7);
        margin: 100px auto;
        width: 160px;
        text-align: center;

        -webkit-transition: all .1s ease;
        -moz-transition: all .1s ease;
        -ms-transition: all .1s ease;
        -o-transition: all .1s ease;
        transition: all .1s ease;
    }

    span#push_me:active {
        -webkit-box-shadow: 0px 3px 0px rgb(10, 100, 255), 0px 3px 6px rgba(0,0,0,.9);
        -moz-box-shadow: 0px 3px 0px rgb(10, 100, 255), 0px 3px 6px rgba(0,0,0,.9);
        box-shadow: 0px 3px 0px rgb(10, 100, 255), 0px 3px 6px rgba(0,0,0,.9);
        position: relative;
        top: 6px;
    }
</style>

<div class="content-block">
    <br>
    <br>
    <br>
    <br>
    <p>
        按住下面按钮，进行语音控制。
    </p>

    <div >
        <span id="push_me" > 声控 </span>
        <div class="debug_output">
        <span id="log">

        </span>
        </div>
    </div>
</div>


<script>
    DEBUG = false;
    window.onerror=function(){
        if(DEBUG){
            alert(JSON.stringify(arguments));
        }
    };
    //录音处理
    var audio_name = "_doc/audio/input.amr";
    // H5 plus事件处理
    function plusReady(){
        log.log("plusReady");
    }
    if(window.plus){
        plusReady();
    }else{
        document.addEventListener('plusready', plusReady, false);
    }
    // 开始录音
    var r=null,t=0,ri=null,rt=null;
    function startRecord(){
        log.log("startRecord");
        r = plus.audio.getRecorder();
        if ( r == null ) {
            outLine('录音对象未获取');
            return;
        }
        r.record({filename:audio_name}, function(p){
            outLine('录音完成：'+p);
            plus.io.resolveLocalFileSystemURL(p, function(entry){

            }, function(e){
                outLine('读取录音文件错误：'+e.message);
            });
        }, function(e){
            outLine('录音失败：'+e.message);
        } );
    }
    // 停止录音
    function stopRecord(){
        log.log("stopRecord");
        clearInterval(ri);
        ri = null;
        r.stop();
        w = null;
        r = null;
    }


    function read(text){
        var a = new Audio();
        a.src = "http://ctrl_v1.yangakw.cn/read?msg="+text;
        a.play();
    }

    function sendCmd(id){
        var up_url = "http://ctrl_v1.yangakw.cn/client/client/sendCmd?id="+id;
        var wt = plus.nativeUI.showWaiting();
        var task = plus.uploader.createUpload(up_url, {
                method: "POST"
            },
            function(resp, status) { //上传完成
                wt.close();
                if(status == 200){
                    var resp_text = resp.responseText;
                    outLine(resp_text);
                    var data = JSON.parse(resp_text);
                    if(data.msg){
                        read(data.msg);
                    }
                }else{
                    outLine("status=>"+status+JSON.stringify(resp));
                }
            }
        );
        task.start();
    }
    function upload_audio(user_id) {
        if(!user_id){
            user_id = 123;
        }
        outLine("upload_audio");
        var up_url = "http://ctrl_v1.yangakw.cn/speech";
        var wt = plus.nativeUI.showWaiting();
        var task = plus.uploader.createUpload(up_url, {
                method: "POST"
            },
            function(resp, status) { //上传完成
                wt.close();
                if(status == 200){
                    var resp_text = resp.responseText;
                    outLine(resp_text);
                    var data = JSON.parse(resp_text);
                    successCB(data);
                }else{
                    outLine("status=>"+status+JSON.stringify(resp));
                }
            }
        );
        task.addFile( audio_name, {key:"audio"} );
        task.start();
    }

    function successCB(data){
        var msg = data.msg;
        read(msg);
    }
    //事件处理
    var div = document.getElementById("push_me");
    var log = document.getElementById("log");
    log.log=function(msg){
        var myDate = new Date();
        var date= myDate.toLocaleString();
        if(DEBUG){
            if(typeof msg != "string"){
                msg = JSON.stringify(msg);
            }
            log.innerHTML = date+"<br/>"+msg+"<br/>"+log.innerHTML;
        }
    };
    outLine = log.log;
    function ondown(){
        log.log("ondown");
        startRecord();
    }
    function onup(){
        stopRecord();
        log.log("onup");
        upload_audio();
    }
    div.ontouchstart = ondown;
    div.ontouchend  = onup ;
    div.onmousedown = ondown;
    div.onmouseup  = onup ;
</script>